#pragma once
#include <iostream>
#include <time.h>
#include <stdarg.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>

#define SIZE 1024
#define Info 0
#define Debug 1
#define Warning 2
#define Error 3
#define Fatal 4

#define Screen 1
#define Onefile 2
#define Classfile 3
#define LogFile "log.txt"

class Log
{
private:
    int printMethod = Screen;       //默认打印到屏幕上                                              
    std::string path = "./log/";    //默认当前路径

public:
    Log(){}
    ~Log(){}

    void Enable(int method){ printMethod = method; }                            //填充打印方法(屏幕, 单独的日志, 同类level的日志)

    std::string levelToString(int level);                                       //将对应等级转换为字符串        

    void printLog(int level, const std::string &logtxt);                        //根据打印方法来打印日志

    void printOneFile(const std::string &logname, const std::string &logtxt);   //打印单独的日志

    void printClassFile(int level, const std::string &logtxt);                  //把level一致的日志放一起
    
    void operator()(int level, const char *format, ...);                        //仿函数-->给外面使用的

};


//将对应等级转换为字符串      
std::string Log::levelToString(int level)
{
    switch (level)
    {
    case Info:
        return "Info";
    case Debug:
        return "Debug";
    case Warning:
        return "Warning";
    case Error:
        return "Error";
    case Fatal:
        return "Fatal";
    default:
        return "None";
    }
}

//根据不同方法来打印日志
void Log::printLog(int level, const std::string &logtxt)
{
    switch (printMethod)
    {
    case Screen:
        std::cout << logtxt << std::endl;
        break;
    case Onefile:
        printOneFile(LogFile, logtxt);
        break;
    case Classfile:
        printClassFile(level, logtxt);
        break;
    default:
        break;
    }
}

//打印单独的日志
void Log::printOneFile(const std::string &logname, const std::string &logtxt)    //把日志打印到文件里
{
    std::string _logname = path + logname;
    int fd = open(_logname.c_str(), O_WRONLY | O_CREAT | O_APPEND, 0666); // "log.txt", 若没有这个文件就创建
    if (fd < 0) return;
        
    write(fd, logtxt.c_str(), logtxt.size());
    close(fd);
}

//把level一致的日志放一起
void Log::printClassFile(int level, const std::string &logtxt)
{
    std::string filename = LogFile;
    filename += ".";
    filename += levelToString(level); // "log.txt.Debug/Warning/Fatal"
    printOneFile(filename, logtxt);
}

//给外面调用的--来打印日志
void Log::operator()(int level, const char *format, ...)
{
    time_t t = time(nullptr);
    struct tm *ctime = localtime(&t);

    //填充缓冲区1: 日志级别 时间
    char leftbuffer[SIZE];
    snprintf(leftbuffer, sizeof(leftbuffer), "[%s][%d-%d-%d %d:%d:%d]", levelToString(level).c_str(),
                ctime->tm_year + 1900, ctime->tm_mon + 1, ctime->tm_mday,
                ctime->tm_hour, ctime->tm_min, ctime->tm_sec);

    //填充缓冲区2: 传来的字符串
    va_list s;
    va_start(s, format);
    char rightbuffer[SIZE];
    vsnprintf(rightbuffer, sizeof(rightbuffer), format, s);
    va_end(s);

    // 日志级别 时间 传来的字符串
    char logtxt[SIZE * 2];
    snprintf(logtxt, sizeof(logtxt), "%s %s", leftbuffer, rightbuffer);

    // printf("%s", logtxt); // 暂时打印
    printLog(level, logtxt);
}

//定义全局对象-->打印日志
Log lg;
